// Registry.cpp: implementation of the Registry class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "Registry.h"

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

Registry::Registry(const std::string& strEntry):
	m_strEntry(strEntry)
{
}

Registry::~Registry()
{
}

DWORD Registry::Read(const std::string& strKeyName, const std::string& strValueName, DWORD dwDefault)
{
	CRegKey RegKey;

	DWORD dwValue;

	std::string str = m_strEntry;
	str += strKeyName;
	if (RegKey.Open(HKEY_LOCAL_MACHINE, str.c_str()) == ERROR_SUCCESS)
	{
		if (RegKey.QueryValue(dwValue, strValueName.c_str()) != ERROR_SUCCESS)
		{
			dwValue = dwDefault;
		}
		RegKey.Close();
	}
	else
	{
		dwValue = dwDefault;
	}

	return dwValue;
}

std::string Registry::Read(const std::string& strKeyName, const std::string& strValueName, const std::string& strDefault)
{
	CRegKey RegKey;

	DWORD dwCount = 256;
	char * pszValue = new char[dwCount];

	m_strValue = strDefault;

	std::string str = m_strEntry;
	str += strKeyName;
	if (RegKey.Open(HKEY_LOCAL_MACHINE, str.c_str()) == ERROR_SUCCESS)
	{
		if (RegKey.QueryValue(pszValue, strValueName.c_str(), &dwCount) == ERROR_SUCCESS)
		{
			m_strValue = pszValue;
		}
		RegKey.Close();
	}

	delete pszValue;

	return m_strValue;
}

void Registry::Write(const std::string& strKeyName, const std::string& strValueName, DWORD dwValue)
{
	CRegKey RegKey;

	std::string str = m_strEntry;
	str += strKeyName;

	if (RegKey.Create(HKEY_LOCAL_MACHINE, str.c_str()) == ERROR_SUCCESS)
	{
		RegKey.SetValue(dwValue, strValueName.c_str());
		RegKey.Close();
	}
}

void Registry::Write(const std::string& strKeyName, const std::string& strValueName, const std::string& strValue)
{
	CRegKey RegKey;

	std::string str = m_strEntry;
	str += strKeyName;

	if (RegKey.Create(HKEY_LOCAL_MACHINE, str.c_str()) == ERROR_SUCCESS)
	{
		RegKey.SetValue(strValue.c_str(), strValueName.c_str());
		RegKey.Close();
	}
}

void Registry::DeleteKey(const std::string& strKeyName)
{
	CRegKey RegKey;

	if (RegKey.Open(HKEY_LOCAL_MACHINE, m_strEntry.c_str())==ERROR_SUCCESS)
	{
		RegKey.DeleteSubKey(strKeyName.c_str());
		RegKey.Close();
	}
}